package com.eastidea.live.controller.rest;

import java.io.IOException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.bson.types.ObjectId;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastidea.live.dao.AppDao;
import com.eastidea.live.dao.FormDao;
import com.eastidea.live.dao.UserDao;
import com.eastidea.live.entity.AppApplication;
import com.eastidea.live.entity.FrmInfMultipleChoice;
import com.eastidea.live.entity.FrmInfMultipleOptions;
import com.eastidea.live.entity.FrmInfSingleLineText;
import com.eastidea.live.entity.FrmUiField;
import com.eastidea.live.entity.FrmUiForm;
import com.eastidea.live.field.multiplechoice.MultipleChoiceDao;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOptionsDao;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOptionsTo;
import com.eastidea.live.field.multiplechoice.MultipleChoiceOt;
import com.eastidea.live.field.multiplechoice.MultipleChoiceService;
import com.eastidea.live.field.multiplechoice.MultipleChoiceTo;
import com.eastidea.live.field.pagebreak.PageBreakOt;
import com.eastidea.live.field.pagebreak.PageBreakService;
import com.eastidea.live.field.pagebreak.PageBreakTo;
import com.eastidea.live.field.singleline.SingleLineTextDao;
import com.eastidea.live.field.singleline.SingleLineTextOt;
import com.eastidea.live.field.singleline.SingleLineTextService;
import com.eastidea.live.field.singleline.SingleLineTextTo;
import com.eastidea.live.ot.FormOt;
import com.eastidea.live.service.FormService;
import com.eastidea.live.to.AppTo;
import com.eastidea.live.to.FieldTo;
import com.eastidea.live.to.FormTo;
import com.eastidea.live.util.Constant;
import com.eastidea.live.util.QrUtil;
import com.eastidea.live.util.RequestUtil;
import com.eastidea.live.util.StringUtil;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.mongodb.Mongo;

@Controller
@RequestMapping(value = Constant.API_REST_V1 + "/fields")
public class FieldRestController {

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(ModelMap modelMap,
			HttpServletRequest request) throws Exception {
		Map<String, Object> res = new HashMap<String, Object>();

		Mongo mongo = new Mongo();
		DB db = mongo.getDB("palmio");
//		boolean auth = db.authenticate("admin", "P2ssw0rd".toCharArray());
//		System.out.println("auth: "+auth);
		DBCollection fields = db.getCollection("field");
		DBCursor cur = fields.find();

		List<DBObject> ls = cur.toArray();
		res.put("flds", ls);
		return res;
	}
}
